package com.example.fyptest3;

import java.io.File;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Display;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class PhotoVideoGalleryActivity extends Activity implements View.OnClickListener, OnTouchListener{

	//private static final int IMAGE_MAX_SIZE = 0;
	private static final int IMAGE_DEFAULT_SIZE = 200;	
	public static File file[];
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setTitle("iMotivate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photo_video_gallery);
		
		//change title bar color to CYAN
		ActionBar bar = getActionBar();
		bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00C4CD")));	
		
		//change title bar text color
		int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
		if (actionBarTitleId > 0) {
		    TextView title = (TextView) findViewById(actionBarTitleId);
		    if (title != null) {
		        title.setTextColor(Color.WHITE);
		    }
		}
		
		//READ IMAGES FROM DEVICE STORAGE, DECODE THEM, AND RENDER ON DISPLAY--------------
		TableLayout tl01 = (TableLayout)findViewById(R.id.tableLayout01);
		
		//get image paths from directory
		String path = Environment.getExternalStorageDirectory().toString()+"/dcim/celeb";
		File f = new File(path);        
		file = f.listFiles();
		
		//create imageviews
		ImageView ivArr[] = new ImageView[file.length];
		BitmapFactory.Options options=new BitmapFactory.Options();
		options.inSampleSize = 8;
		Bitmap bm = null;
		
		//get device dimensions
		int dimens = IMAGE_DEFAULT_SIZE;
		float density = this.getResources().getDisplayMetrics().density;
		int finalDimens = (int)(dimens * density);	
		Display display = getWindowManager().getDefaultDisplay();
		
		//calculate number of images and number of images per row
		int nImagesPerRow = display.getWidth()/(IMAGE_DEFAULT_SIZE); //3
		int nR = file.length%nImagesPerRow;
		int nRows = file.length/nImagesPerRow; //13
		if(nR!=0)nRows++;
		
		//place images in table layout
		TableRow[] tr = new TableRow[nRows];
		tr[0] = new TableRow(this);
		LinearLayout[] ll01 = new LinearLayout[nRows];
		ll01[0] = new LinearLayout(this);
		ll01[0].setOrientation(LinearLayout.HORIZONTAL);
		
		tr[0].addView(ll01[0]);
		tl01.addView(tr[0]);
		int countRows = 0;
		ivArr[0] = new ImageView(this);
		
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(finalDimens, finalDimens);
		layoutParams.setMargins(2, 2, 2, 2);
		
		for (int i=0; i < file.length; i++){
			ivArr[i] = new ImageView(this);
			ivArr[i].setId(i);
			
			bm = ImageDecode.staticDecodeFile(file[i]);
//Toast.makeText(PhotoVideoGalleryActivity.this, file[i].toString(), Toast.LENGTH_LONG).show();
			
			ivArr[i].setImageBitmap(bm);
			ivArr[i].setLayoutParams(layoutParams);
			
			ivArr[i].setScaleType(ImageView.ScaleType.CENTER_CROP);
			
			ll01[countRows].addView(ivArr[i]);
			if(i!=0 && i%nImagesPerRow==0){
				
				countRows++;
				ll01[countRows] = new LinearLayout(this);
				tr[countRows] = new TableRow(this);
				tr[countRows].addView(ll01[countRows]);
				tl01.addView(tr[countRows]);
				
			}
		}
		
		
		
		//BUTTON_ALBUM----------------------------
		Button button_album = (Button)findViewById(R.id.button_album);
		button_album.setOnClickListener(new View.OnClickListener() {	
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent myIntent = new Intent(PhotoVideoGalleryActivity.this, AlbumActivity.class);
				startActivity(myIntent);				
			}
		});
	
		//initialize the click on listener on imageviews
		for(int i=0; i<ivArr.length; i++){
			ivArr[i].setOnClickListener(this);
		}		
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_photo_video_gallery, menu);
		return true;
	}



	//show image in full screen when clicked
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(this, FullScreenImageActivity.class);
		String path = file[v.getId()].toString();
		String image_n = file[v.getId()].getName();
		intent.putExtra("image_path", path);
		intent.putExtra("image_name", image_n);
		intent.putExtra("image_pos", v.getId());
		startActivity(intent);
	}


	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		// TODO Auto-generated method stub
		return false;
	}

		
//Toast.makeText(getApplicationContext(), Integer.toString(ivArr[0].getId()), Toast.LENGTH_LONG).show();	
//END
}
